var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "categoryId",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            name: "categoryName",
            url:"nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        materialCategory:{
            parentName:null,
            parentId:0,
            categoryOrder:0
        }
    },
    methods: {
        getMaterialCategory: function(){
            //加载菜单树
            $.get(baseURL + "material/materialcategory/select", function(r){
                ztree = $.fn.zTree.init($("#materialCategoryTree"), setting, r.materialCategoryList);
                var node = ztree.getNodeByParam("categoryId", vm.materialCategory.parentId);
                ztree.selectNode(node);

                vm.materialCategory.parentName = node.categoryName;
            })
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.materialCategory = {parentName:null,parentId:0,categoryOrder:0};
            vm.getMaterialCategory();
        },
        update: function () {
            var categoryId = getMaterialCategoryId();
            if(categoryId == null){
                return ;
            }

            $.get(baseURL + "material/materialcategory/info/"+categoryId, function(r){
                vm.showList = false;
                vm.title = "修改";
                vm.materialCategory = r.materialCategory;

                vm.getMaterialCategory();
            });

        },
        saveOrUpdate: function (event) {
            var url = vm.materialCategory.categoryId == null ? "material/materialcategory/save" : "material/materialcategory/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.materialCategory),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(index){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var categoryId = getMaterialCategoryId();
            if(categoryId == null){
                return ;
            }

            confirm('确定要删除选中的记录？', function(){
                $.ajax({
                    type: "POST",
                    url: baseURL + "material/materialcategory/delete",
                    data: "categoryId="+categoryId,
                    success: function(r){
                        if(r.code == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        materialCategoryTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择耗材分类",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#materialCategoryLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级菜单
                    vm.materialCategory.parentId = node[0].categoryId;
                    vm.materialCategory.parentName = node[0].categoryName;

                    layer.close(index);
                }
            });
        },
        reload: function () {
            vm.showList = true;
            MaterialCategory.table.refresh();
        },
    }
});

var MaterialCategory = {
    id: "materialCategoryTable",
    table: null,
    layerIndex: -1
};


/**
 * 初始化表格的列
 */
MaterialCategory.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '分类ID', field: 'categoryId', visible: false, align: 'center', valign: 'middle', width: '80px'},
        {title: '耗材分类名称', field: 'categoryName', align: 'center', valign: 'middle', sortable: true, width: '180px'},
        {title: '父级分类', field: 'parentName', align: 'center', valign: 'middle', sortable: true, width: '100px'}
    ]
    return columns;
};

function getMaterialCategoryId () {
    var selected = $('#materialCategoryTable').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        alert("请选择一条记录");
        return null;
    } else {
        return selected[0].id;
    }
}

$(function () {
    var colunms = MaterialCategory.initColumn();
    var table = new TreeTable(MaterialCategory.id, baseURL + "material/materialcategory/list", colunms);
    table.setExpandColumn(2);
    table.setIdField("categoryId");
    table.setCodeField("categoryId");
    table.setParentCodeField("parentId");
    table.setExpandAll(false);
    table.setHeight(700)
    table.init();
    MaterialCategory.table = table;
});